package com.example.springbootnetty.socket.listerner;

import com.corundumstudio.socketio.SocketIOServer;
import com.example.springbootnetty.socket.ChatHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class StartListener implements ApplicationListener<ApplicationReadyEvent> {
    private final SocketIOServer server;
    private final ChatHandler chatHandler;
    @Autowired
    public StartListener(SocketIOServer server, ChatHandler chatHandler) {
        this.server = server;
        this.chatHandler = chatHandler;
    }

    /**
     * Handle an application event.
     *
     * @param event the event to respond to
     */
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("asddsa");
        new Thread(()->{
            System.out.println("发送线程启动");
            while (true)   {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                server.getBroadcastOperations().sendEvent("gameMessage",chatHandler.getUserPosition());
            }

        }).start();

    }
}
